<template>
  <div class="visit_box">
    <div ref="visitCharts" class="visit"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";
export default {
  props:{
    visit_data:{
      type:Object,
      required: true,
      default:{}
    }
  },
  data() {
    return {};
  },
  mounted() {
    this.initChart();
    window.addEventListener("resize", this.resizeChart);
  },
  beforeUnmount() {
    window.removeEventListener("resize", this.resizeChart);
    if (this.chart) {
      this.chart.dispose();
    }
  },
  methods: {
    // fetchTouristData() {
    //   this.$axios.get('/api/index/touristStayTime')
    //     .then(response => {
    //       if (response.data.code === 1) {
    //         this.touristData = response.data.data;
    //         this.initChart();
    //       } else {
    //         console.error('Failed to fetch data:', response.data.msg);
    //       }
    //     })
    //     .catch(error => {
    //       console.error('API request failed:', error);
    //     });
    // },
    initChart() {
      this.chart = echarts.init(this.$refs.visitCharts);
      const option = this.getChartOption();
      this.chart.setOption(option);
    },
    getChartOption() {
      return {
        title: {
          text: "",
          axisLabel: {
            align: "right",
            color: "#fff",
            fontSize: 22,
          },
          top: "5%",
          left: "center",
        },
        grid: {
          top: "18%",
          bottom: "10%",
          left: "10%",
          right: "0",
        },
       
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        xAxis: {
          type: "category",
          data: this.visit_data.time,
          axisLabel: {
            //坐标轴刻度标签的相关设置。
            color: "#fff",
            fontStyle: "normal",
            fontSize: 15,
          },
          axisTick: {
            //坐标轴刻度相关设置。
            show: false,
          },
          axisLine: {
            //坐标轴轴线相关设置
            lineStyle: {
              color: "#fff",
              opacity: 0.2,
            },
          },
          splitLine: {
            //坐标轴在 grid 区域中的分隔线。
            show: false,
          },
        },
        yAxis: [
          {
            type: "value",
            min: 0,
            // max: this.visit_data.browse_max,
            axisTick: {
              show: false,
            },
            axisLine: {
              lineStyle: {
                color: "#fff",
              },
            },
            axisLabel: {
              color: "#fff",
              fontSize: 10,
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: "dashed",
                color: "#0c2c5a",
              },
            },
          },
        ],
        series: [
          {
            type: "line",
            name: "人数",
            smooth: true, //平滑曲线显示
            // symbol: "circle", //标记的图形为实心圆
            symbolSize: 8,
            itemStyle: {
              normal: {
                color: "rgb(59,196,236)",
                lineStyle: {
                  color: "rgb(59,196,236)",
                },
              },
            },
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 0,
                      color: "rgba(34,77,136,0.5)",
                    },
                    {
                      offset: 0.5,
                      color: "rgba(34,77,136,.3)",
                    },
                    {
                      offset: 1,
                      color: "rgba(34,77,136,0)",
                    },
                  ],
                  false
                ),
              },
            },
            label: {
              show: false,
              distance: 1,
            },
            data: this.visit_data.data ,
          },
        ],
      };
    },
    resizeChart() {
      if (this.chart) {
        this.chart.resize();
      }
    },
  },
  watch:{
    visit_data(){
      this.initChart()
    }
  }
};
</script>

<style lang="scss" scoped>
.visit_box {
  position: relative;
  width: 100%;
  height: 238px;
}
.visit {
  width: 100%;
  height: 238px;
  margin-left: 15px;
}
.visit_title {
  position: absolute;
  top: 3px;
  right: 0;
  z-index: 10;
  display: flex;
  color: #ccc;
  width: 258px;
  height: 30px;
  div {
    background: rgba(216, 240, 255, 0.1);
    /* width: 104px; */
    flex: 1;
    height: 100%;
    font-size: 15px;
    line-height: 30px;
    margin-left: 10px;
    position: relative;
    p {
      width: 104px;
    }
    img {
      width: 8px;
      height: 8px;
      position: absolute;
      top: 3px;
      right: 5px;
    }
  }
}
</style>